<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%
	String hospitalNotesJson= (String)request.getAttribute("hospitalNotesJson");
	String pageJson= (String)request.getAttribute("pageJson");
 %>


<!--内容部分-->

    
<div class="list_NR">

    <div class="loc">
        <h3>数据提取</h3>
       <!--  <ul class="loc_loc">
          	  病案归档 > 未归病案
        </ul> -->
    </div>
    
      <div class="list_con_table">
        <input type="hidden" value="${medRecordQuery.totalCounts }" id="totalCountsHidden">
        <input type="hidden" id="basePath" value="${basePath }" />
		<form action="${basePath}/medrecord/unPigeonhole" method="get" id="queryForm">
		<input type="hidden" name="currentPage" value="${medRecordQuery.currentPage }" id="currentPageHidden">
    	<input type="hidden" name="pageSize" value="${medRecordQuery.pageSize }" id="pageSizeHidden">
    	<div class="search_table">
           <ul class="clearfix" style="margin-bottom:0;">

            <li class="Label_1" style="width:11%">出院日期：&nbsp;</li>
               <li class="Label_2" style="width:27%;">
                   <input style="width:42%;" class="input_box" id="startDate" value='' onFocus="WdatePicker()" type="text" > 
                   - <input style="width:42%" class="input_box" id="endDate"  value='' onFocus="WdatePicker()" type="text" >
               </li>
               <li class="Label_1" style="width:11%">ID号：&nbsp;</li>
               <li class="Label_2">
               	<input class="input_box" type="text"    id="mrId" value='' />
               </li>
               <li class="Label_1" style="width:11%">住院次数：&nbsp;</li>
               <li class="Label_2">
               	<input class="input_box" id="visitNumber" type="text"  id="visitNumber" value='' />
               </li>
                <li class="Label_1" style="width:11%">出院科室：&nbsp;</li>
               <li class="Label_2">
               		<select class="input_box" id="outDeptCode" name="outDeptCode"></select>
               </li>
              
                <li class="Label_5">
                    <div class="right">
                    	<a id="Add" onclick="queryForm(1,10)">查询</a>
                    </div>
                    
                    <div class="right">
                		 <a id="addCheckAllMedial" >勾选提取</a>
                	</div>
                    
                </li>
            </ul>
        </div>
     </form>

   </div>

   <table class="Search_tableList"cellpadding="0" cellspacing="0">

       <tr>
       	  <td class="tdLabel_4">全选<input type="checkbox" class="check_cl" id ="checkAll" style="margin-left: 10px; margin-top: 8px;"></td>
           <td class="tdLabel_4">姓名</td>
           <td class="tdLabel_4">身份证号</td>
           <td class="tdLabel_4">ID号</td>
           <td class="tdLabel_4">住院次数</td>
           <td class="tdLabel_4">出院日期</td>
           <td class="tdLabel_4">出院科室</td>
           <td class="tdLabel_4">状态</td>
           <td class="tdLabel_4">操作</td>
       </tr>
        <tr id="loading" style="height:50px;display:none">
	             <td colspan="9" align="center" ><img src="${basePath}/static/image/loading-2.gif"></td>
	    </tr>
       <tbody id="tbody">
      
        </tbody>
           
       
    </table>

    <div id="pageList" class="pageList" hidden="true">
    	<input type="hidden" id="current" value="1">
        <ul class="pagination clearfix" id="page_plus"></ul>
        <div class="pagination" style="position: relative;">
            <div>总共：<b id="totalPage">0</b> 条信息    当前页是第 <b id="currentPage">0/0</b>　页
            </div>
        </div>
    </div>
</div>

<!--script JS部分-->
    
<script type="text/javascript" src="${basePath}/static/publics/assets/jquery/jquery.min.js"></script>
<script type="text/javascript" src="${basePath}/static/publics/assets/bootstrap/js/bootstrap.js"></script>
<script type="text/javascript" src="${basePath}/static/publics/assets/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${basePath}/static/publics/assets/jqPaginator/jqPaginator.js"></script>
<script type="text/javascript">

	$("#addCheckAllMedial").click(function() {
		var length = $(".noPushMedical:checked").length;
		if (length == 0) {
			layer.msg("请您先勾选要提取的病案!");
			return;
		}

		//添加病案数据
		var params = "";
		$(".noPushMedical:checked").each(function(index) {
			var mrId = $(this).parent().next().next().next().text();
			var visitNumber = $(this).parent().next().next().next().next().text();
			if (index != length - 1) {
				params+=mrId+"-"+visitNumber+"--";
			} else {
				params+=mrId+"-"+visitNumber;	
			}
		});
		//console.log(params);
		pullData(params);
	});

	//选择所有或取消选择所有
	$("#checkAll").click(function() {
		if ($(this).is(":checked")) {
			$(".noPushMedical").each(function(index) {
				$(this).prop("checked", true);
			});
		} else {
			$(".noPushMedical").each(function(index) {
				$(this).prop("checked", false);
			});
		}
	});

	//查询oracle中数据
	function queryForm(pageNo,pageSize){
		var startDate=$("#startDate").val();
		var endDate=$("#endDate").val();
		var mrId=$("#mrId").val();
		var visitNumber=$("#visitNumber").val();
		var outDeptCode=$("#outDeptCode").val();
		//alert(startDate+"--"+endDate+"--"+mrId+"--"+visitNumber);
		if(!startDate && !endDate  && !mrId && !visitNumber&&!outDeptCode){
			layer.alert('查询条件不能为空!',{icon:7},function(){
				$("#mrId").val("").focus();
				$("#visitNumber").val("");
				layer.closeAll();
			});
			return false;
		}
		
		jQuery.support.cors = true;
		$.ajax({
			type:"get",
		     //url:"http://localhost:28088/medical-export/export305/ajaxExportMedicalVisitJson",  
			 url:"http://132.147.0.48:8090/medical-export/export305/ajaxExportMedicalVisitJson", 
			data:{
				startDate:startDate,endDate:endDate,mrId:mrId,visitNumber:visitNumber,outDeptCode:outDeptCode,pageNo:pageNo,pageSize:pageSize
			},
			beforeSend:function(){
    	 		$("#tbody").html("");
    	 		$("#loading").show();
    	 	},
			success:function(data){
				$("#loading").hide();
				if(data.success){
					var pageInfo =data.data;
					var dataList = pageInfo.list;
					$("#pageList").show();
					if(pageInfo.total>0){
						page(pageInfo.pageNum,pageInfo.total);
					}else{
						$("#page_plus").html("");
						$("#totalPage").html(0);
						$("#currentPage").html(0 + "/" + 0);
					}
					//page(pageInfo.pageNum,pageInfo.total);
					var html="";
					for(var i=0 ;i<dataList.length;i++){
						
						var obj=dataList[i];
						var checkStr = "";
						if (obj.new_record == 1) {
							if ($("#checkAll").is(":checked")) {
								checkStr = "<td class='tdLabel_5'><input type='checkbox' checked class='check_cl noPushMedical'></td>";
							} else {
								checkStr = "<td class='tdLabel_5'><input type='checkbox' class='check_cl noPushMedical'></td>";
							}
						} else {
							
							checkStr = "<td class='tdLabel_5'></td>";
						}
						
						html+="<tr>"+ checkStr+
					          " <td class='tdLabel_5'>"+(obj.PATIENTNAME==undefined?'':obj.PATIENTNAME)+"</td>"+
					          " <td class='tdLabel_5'>"+(obj.IDNUMBER==undefined?'':obj.IDNUMBER)+"</td>"+
					          " <td class='tdLabel_5'>"+(obj.MRID==undefined?'':obj.MRID)+"</td>"+
					          " <td class='tdLabel_5'>"+(obj.VISITNUMBER==undefined?'':obj.VISITNUMBER)+"</td>"+
					          " <td class='tdLabel_5'>"+(obj.OUTHOSPITALDATETIME==undefined?'':obj.OUTHOSPITALDATETIME)+"</td>"+
					          " <td class='tdLabel_5'>"+(obj.OUTDEPTNAME==undefined?'':obj.OUTDEPTNAME)+"</td>"+
					          " <td class='tdLabel_5'>"+(obj.new_record==0?'已提取':'未提取')+"</td>"+
					          " <td class='tdLabel_5'>"+(obj.new_record==0?'':"<a mrId='"+obj.MRID+"' visitNumber='"+obj.VISITNUMBER+"' onclick='singleFetchData(this)' class='int'>提取</a>")
					          	+"<a mrId='"+obj.MRID+"' visitNumber='"+obj.VISITNUMBER+"' onclick='checkNurseInfo(this)' class='int'>护理单核对</a></td>"+
					       "</tr>";
					}
					
					$("#tbody").html(html);
				}else{
					layer.alert(data.msg,{icon:2});
				}
			},
			error:function(){
				$("#loading").hide();
				layer.alert('服务器异常!',{icon:2});
			}
		})
	}
	
	//逐个提取数据
	function singleFetchData(obj){
		var $this=$(obj);
		var mrId=$this.attr("mrId");
		var visitNumber=$this.attr("visitNumber");
		//alert(mrId+"--"+visitNumber);
		var params="";
		params+=mrId;
		params+="-";
		params+=visitNumber;

		pullData(params,mrId,visitNumber);
		
	}
	
	//提取病案数据
	function pullData(params,mrId,visitNumber){
		var loadingIndex = null;
		jQuery.support.cors = true;
		$.ajax({
			type:"get",
			url:"http://132.147.0.48:8090/medical-export/export305/ajaxFetchDataByMrIdAndVisitNumber", 
			//url:"http://localhost:28088/medical-export/export305/ajaxFetchDataByMrIdAndVisitNumber", 
			data:{params:params},
			 //dataType:"json",
			// dataType: 'jsonp',
			 //contentType : 'application/json;charset=utf-8', //设置请求头信息  
			beforeSend:function(){
    	 		//$("#tbody").html("");
				loadingIndex = layer.load(1, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
         		});
    	 	},
			 success:function(data){
				 layer.close(loadingIndex);
				 if(data.success) {
					var pageNo=$("#current").val();
					queryForm(pageNo,10);
					bindMedicalRecordImage(mrId,visitNumber);
				 } else {
					 layer.alert(data.msg, {icon:2});
				 }
				
			},error:function(XMLHttpRequest, textStatus, errorThrown) {
				layer.close(loadingIndex);
				layer.alert("系统异常!", {icon:2});
             	console.log("-----提取病案失败----" + errorThrown);
       }  //error end 

		})
	}
	
	
	function bindMedicalRecordImage(mrId,visitNumber){
		var basePath = $("#basePath").val();
		var params = {mrId:mrId,visitNumber:visitNumber}
		$.ajax({
			type:"post",
			url:basePath+"/bind/bind_medical_record_image", 
			//url:"http://localhost:28088/medical-export/export305/ajaxFetchDataByMrIdAndVisitNumber", 
			data:params,
			success:function(data){
				console.log(data);
				
			}
		})
		
	}
	
	$("#mrId").change(function(){
		var $this=$(this);
		var inentity=$this.val();
		if(inentity!=null && inentity!='' && inentity!=undefined){
			if(isContainMark(inentity)){
				var mrId=inentity.split("-")[0];
				var visitNumber=inentity.split("-")[1];
				
				if(!isNumber(visitNumber)){
					layer.alert("请重新扫描条形码",{icon:7},function(){
						$("#mrId").val("").focus();
						$("#visitNumber").val("");	
						layer.closeAll();
					});
					return false;
				}
				//alert(mrId+"--"+visitNumber);
				$("#mrId").val("").val(mrId);
				$("#visitNumber").val("").val(visitNumber);
				queryForm(1,10);
			}
		}
	});
	
	function isNumber(value) {
	    var patrn = /^[0-9]*$/;
	    if (patrn.exec(value) == null || value == "") {
	        return false
	    } else {
	        return true
	    }
	}
	
	
	function isContainMark(inentity){
		var reg = RegExp(/-/);
		return reg.test(inentity);
	}

	$(function(){
		$("select").each(function(){
			$(this).append("<option value=''>---请选择---</option>");
			
		});
		var basePath = $("#basePath").val();
		
		$.ajax({
			url: basePath + "/baseinfo/ajax_medical_offices",
			type: "GET",
			data:{hospitalId:1},
			dataType: "json",
			success: function( datas ) {
				var medicalDepts = datas['medicalOfficesList'];
				$("select[name='outDeptCode']").each(function(){
					var options = "";
					for(var index in medicalDepts){
						var medicalDept = medicalDepts[index];
				    	options+="<option value='"+medicalDept.code+"'>"+medicalDept.name+"</option>"
				    } 
			       $(this).append(options);
				});
			}
		});
		$("#mrId").focus();
		
		
		
	});
	
	function page(currentPage, totalCounts) {
		var pageSize = 10;
		var visiblePages = 10;
		var totalPages = Math.ceil(totalCounts / pageSize);
		$('#page_plus').jqPaginator({
			totalCounts : totalCounts,
			pageSize : pageSize,
			visiblePages : visiblePages,
			currentPage : currentPage,
			first : '<li class="first"><a href="javascript:void(0);">首页</a></li>',
			prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
			next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
			last : '<li class="last"><a href="javascript:void(0);">末页</a></li>',
			page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
			onPageChange : function(num, type) {
				if (type != 'init') {
					queryForm(num,pageSize);
				}
				$("#totalPage").html(totalCounts);
				$("#currentPage").html(num + "/" + totalPages);
				$("#current").val(num);
			}
		});
	}
	
	
	
	
	
	function selectAll(){
		  $('input[name="outHospitalTypeIds"]').each(function(){  
	          //此处如果用attr，会出现第三次失效的情况  
	          $(this).prop("checked",true);  
	      });  
	}

	function disableAll(){
		  $('input[name="outHospitalTypeIds"]').each(function(){  
	          //此处如果用attr，会出现第三次失效的情况  
	          $(this).prop("checked",false);  
	      });  
	}
	
	//核对护理记录单
	function checkNurseInfo(obj) {
		var $this=$(obj);
		var mrId=$this.attr("mrId");
		var visitNumber=$this.attr("visitNumber");
		
		var content = "";
		content+='<ul style="padding: 20px; font-size: 20px;">';
		content+="<li style='height: 50px; width: 200px;  float:left;'><a href='javascript:goNurseInfoPdf(1,"+visitNumber+", \""+mrId+"\")'>病重护理记录</a><li>";
		content+="<li style='height: 50px; width: 200px;  float:left;'><a href='javascript:goNurseInfoPdf(2,"+visitNumber+", \""+mrId+"\")'>特别护理记录</a><li>";
		content+="<li style='height: 50px; width: 200px;  float:left;'><a href='javascript:goNurseInfoPdf(3,"+visitNumber+", \""+mrId+"\")'>病危护理记录</a><li>";
		content+="<li style= 'height: 50px;  width: 200px;  float:left;'><a href='javascript:goNurseInfoPdf(4,"+visitNumber+", \""+mrId+"\")'>一般护理记录</a><li>";
		content+="<li style='height: 50px;  width: 200px;  float:left;'><a href='javascript:goNurseInfoPdf(5,"+visitNumber+", \""+mrId+"\")'>体温单</a><li>";
		content+="<li style='height: 50px;  width: 200px;  float:left;'><a href='javascript:goNurseInfoPdf(6,"+visitNumber+", \""+mrId+"\")'>血糖测定</a><li>";
		content+="<li style='height: 50px; width: 200px;  float:left;'><a href='javascript:goNurseInfoPdf(7,"+visitNumber+", \""+mrId+"\")'>心电检测</a><li>";
		content+="<li style='height: 50px;  width: 200px;  float:left;'><a href='javascript:goNurseInfoPdf(8,"+visitNumber+", \""+mrId+"\")'>生命体征观察</a><li>";
		content+="</ul>"; 
		
		layer.open({
		    type: 1,
		    title: '核对护理记录',   //标题
		    area: ['440px', '315px'],   //宽高
		    shade: 0.4,   //遮罩透明度
		    content: content,//支持获取DOM元素
		    btn: ['确定', '关闭'], //按钮组
		    scrollbar: false ,//屏蔽浏览器滚动条
		    yes: function(index){//layer.msg('yes');    //点击确定回调
		        layer.close(index);
		    },
		    btn2: function(){//layer.alert('aaa',{title:'msg title'});  ////点击取消回调
		       layer.closeAll();
		    }
		})
	}
	
	//护理记录单核对--调用远卓的接口
	function goNurseInfoPdf(nurseType, visitNumber, mrId) {
        var remoteUrl = "http://132.147.0.8:8099/"+mrId+"_"+visitNumber+"_";
		//var remoteUrl = "http://localhost:8090/upload/"+mrId+"_"+visitNumber+"_";
		if (nurseType == 1) {
			remoteUrl+="bingzhj.pdf";
		} else if (nurseType == 2) {
			remoteUrl+="tebiehj.pdf";
		} else if (nurseType == 3) {
			remoteUrl+="bingwhj.pdf";
		} else if (nurseType == 4) {
			remoteUrl+="yibanhj.pdf";
		} else if (nurseType == 5) {
			remoteUrl+="tiwen.pdf";
		} else if (nurseType == 6) {
			remoteUrl+="xuetcd.pdf";
		} else if (nurseType == 7) {
			remoteUrl+="xindjc.pdf";
		} else if (nurseType == 8) {
			remoteUrl+="smtzgc.pdf";
		}
		 
		window.open(remoteUrl);		
	}
	
	
</script>





